/*
 * @Author: caoxiang caoxiang@huaqinits.com
 * @Date: 2022-11-10 17:35:04
 * @LastEditors: caoxiang caoxiang@huaqinits.com
 * @LastEditTime: 2022-12-30 15:41:00
 * @FilePath: \admin-web\src\utils\astrict.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
// 单页长时间不操作就会自动退出
var lastTime = new Date().getTime()
var currentTime = new Date().getTime()
var timeOut = 2 * 60 * 60 * 1000 // 设置超时时间: 2小时

window.onload = function () {
  window.document.onmousedown = function () {
    localStorage.setItem('lastTime', new Date().getTime())
  }
}
function delCookie () {
  var cookies = document.cookie.split(';')
  for (var i = 0; i < cookies.length; i++) {
    var cookie = cookies[i]
    var eqPos = cookie.indexOf('=')
    var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie
    document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/'
  }
  if (cookies.length > 0) {
    for (let i = 0; i < cookies.length; i++) {
      const cookie = cookies[i]
      const eqPos = cookie.indexOf('=')
      const name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie
      var domain = location.host.substr(location.host.indexOf('.'))
      document.cookie =
        name +
        '=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; domain=' +
        domain
    }
  }
}
function checkTimeout () {
  currentTime = new Date().getTime() // 更新当前时间
  lastTime = localStorage.getItem('lastTime')
  // console.log(currentTime - lastTime);
  // console.log(timeOut);
  if (currentTime - lastTime > timeOut) {
    // 判断是否超时
    // console.log("超时");
    var url = window.location.href
    // var newUrl = url.match(/(\S*)#/)
    // console.log(newUrl)
    const index = url.lastIndexOf('#')

    if (url.substring(index + 1) !== '/login') {
      if (url.substring(0, index) !== 'http://localhost:8080/') {
        delCookie()
        window.open('/', '_self')
      }
    }
  }
}

/* 定时器 间隔30秒检测是否长时间未操作页面 */
window.setInterval(checkTimeout, 10000)
